package com.hotel.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.hotel.bean.Hotelinfor;
import com.hotel.dao.HotelinDao;
import com.hotel.util.DBUtils;

public class HotellinDaoImpl implements HotelinDao{

	
	@Override
	public boolean insert(Hotelinfor t) throws SQLException {
		return false;
	}

	@Override
	public void deleteById(int id) throws SQLException {
		
	}

	@Override
	public void update(Hotelinfor t) throws SQLException {
		
	}
	//路遥
	@Override
	public Hotelinfor queryByid(int id) throws SQLException {
		Connection conn = DBUtils.getConnection();
		String sql = "select * from hotelinfor where hid = ?";
		PreparedStatement psmt = conn.prepareStatement(sql);
		psmt.setInt(1, id);
		ResultSet rs = psmt.executeQuery();
		Hotelinfor hotel = null;
		while (rs.next()) {
			int hid = rs.getInt("hid");
			String hNewname = rs.getString("hname");
			String harea = rs.getString("harea");
			String htype = rs.getString("htype");
			String hprice = rs.getString("hprice");
			int hbooking = rs.getInt("hbooking");
			int hbreakfast = rs.getInt("hbreakfast");
			String hImg = rs.getString("hImg");
			hotel = new Hotelinfor(hid,hNewname,harea,htype,hprice,hbooking,hbreakfast,hImg);
		}
		
		DBUtils.closeResource(conn, psmt, rs);
		
		return hotel;
	}

	@Override
	public List<Hotelinfor> queryAll() throws SQLException {
		return null;
	}
//路遥
	@Override
	public List<Hotelinfor> queryByName(String hname) throws SQLException {
		Connection conn = DBUtils.getConnection();
		String sql = "select * from hotelinfor where hname like ?";
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setString(1, "%" + hname + "%");
		ResultSet rs = ps.executeQuery();
		
		List<Hotelinfor> hf = new ArrayList<>();
		while (rs.next()) {
			int hid = rs.getInt("hid");
			String hNewname = rs.getString("hname");
			String harea = rs.getString("harea");
			String htype = rs.getString("htype");
			String hprice = rs.getString("hprice");
			int hbooking = rs.getInt("hbooking");
			int hbreakfast = rs.getInt("hbreakfast");
			String hImg = rs.getString("hImg");
			Hotelinfor hotel = new Hotelinfor(hid,hNewname,harea,htype,hprice,hbooking,hbreakfast,hImg);
			hf.add(hotel);
		}
		DBUtils.closeResource(conn, ps, rs);
		return hf;
	}
	//路遥
	@Override
	public List<Hotelinfor> queryByArea(String harea) throws SQLException {
		Connection conn = DBUtils.getConnection();
		String sql = "select * from hotelinfor where harea = ?";
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setString(1, harea);
		ResultSet rs = ps.executeQuery();
		
		List<Hotelinfor> hf = new ArrayList<>();
		while (rs.next()) {
			int hid = rs.getInt("hid");
			String hNewname = rs.getString("hname");
			String hNewarea = rs.getString("harea");
			String htype = rs.getString("htype");
			String hprice = rs.getString("hprice");
			int hbooking = rs.getInt("hbooking");
			int hbreakfast = rs.getInt("hbreakfast");
			String hImg = rs.getString("hImg");
			Hotelinfor hotel = new Hotelinfor(hid,hNewname,hNewarea,htype,hprice,hbooking,hbreakfast,hImg);
			hf.add(hotel);
		}
		DBUtils.closeResource(conn, ps, rs);
		return hf;

	}

}